/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataServices;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JTable;

/**
 *
 * @author DOANTUNGANH
 */
public class LoadingBean {

    public static Connection conn = null;

    public void loadTable(String sql, JTable table) {
        TransactBean transact = new TransactBean();
        ResultSet rs = null;
        try {
            rs = transact.getpreparedStatement(sql).executeQuery();
            table.setModel(new TableModel(rs));
        } catch (SQLException ex) {
            Logger.getLogger(LoadingBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            transact.destroy();
        }
    }

    public void loadTableCallable(String sql, JTable table) {
        TransactBean transact = new TransactBean();
        ResultSet rs = null;
        try {
            rs = transact.getStoreProcedure(sql).executeQuery();
            table.setModel(new TableModel(rs));
        } catch (SQLException ex) {
            Logger.getLogger(LoadingBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            transact.destroy();
        }
    }
    
        public void loadCallable(String sql) {
        TransactBean transact = new TransactBean();
        ResultSet rs = null;
        try {
            transact.getStoreProcedure(sql).executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(LoadingBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            transact.destroy();
        }
    }
    

    public void loadComboBox(String sql, JComboBox comboBox) {
        TransactBean transact = new TransactBean();
        ResultSet rs = null;
        Vector<String> temp = new Vector<String>();
        try {
            rs = transact.getpreparedStatement(sql).executeQuery();
            while (rs.next()) {
                temp.add(rs.getString(1));
                System.out.println(rs.getString(1));
            }
            for (int i = 0; i < temp.size(); i++) {
                comboBox.addItem(temp.elementAt(i));
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoadingBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            transact.destroy();
        }
    }

    public void loadList(String sql, JList list) {
        TransactBean transact = new TransactBean();
        ResultSet rs = null;
        Vector<String> temp = new Vector<String>();
        DefaultListModel model = new DefaultListModel();

        try {
            rs = transact.getpreparedStatement(sql).executeQuery();
            while (rs.next()) {
                temp.add(rs.getString(1));
                System.out.println(rs.getString(1));
            }
            for (int i = 0; i < temp.size(); i++) {
                model.addElement(temp.elementAt(i));
            }
            list.setModel(model);
        } catch (SQLException ex) {
            Logger.getLogger(LoadingBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            transact.destroy();
        }
    }
}
